#!/bin/bash
BaseDir=$(cd "$(dirname "$0")"; pwd)
cd ${BaseDir}

set -x
set -o errexit

# 3 kubesphere hostpath卷只在worker上用[为方便调试分析机器上都建下]
IPNODES="192.168.99.10 192.168.99.11"
for ip in $IPNODES; do
 echo "prepare local volumn ${ip}..."
 ssh -i private_key -o StrictHostKeyChecking=no root@${ip} "rm -rf /opt/data/; mkdir -p /opt/data/{redis,etcd,minio,mysql,openldap,prometues}"
done

#offline
# example: ./install-ks.sh ks/alone
if [ $# -eq 1 ]; then
	echo "offline install kubesphere..."
	# 3.1 load docker images
	if [ "$(ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.10 "docker images|grep kubesphere|wc -l")" = "0" ]; then
	 #方式1：各节点加载自己的
	 if [ "$1" = "alone" ]; then
	 	echo "load docker image alone..."
		ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.10 "cd /vagrant/image;  docker load < kubesphere-dev.tar"
		ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.11 "cd /vagrant/image;  docker load < kubesphere-node1.tar"
	 else
		#方式2 加载 ： imgsave.sh 从kubesphere官方镜像包选取的内容
		for ip in $IPNODES; do
		 	echo "load docker image ${ip}..."
			ssh -i private_key -o StrictHostKeyChecking=no root@${ip} "cd /vagrant/kubesphere3; docker load < kubesphere3-deps.tar"
		done
	 fi
	fi

	# 3.2 install kubesphere
	ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.10 "cd /vagrant/kubesphere3; ./install.sh"
else
	echo "online install kubesphere..."
    #这种模式，需要从仓库获取kubesphere的镜像，安装时间较长
	ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.10 "cd /vagrant/kubesphere3; ./install.sh"
#在线部署好以后，导出镜像，以备离线部署
#	ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.10 "cd /vagrant/image/; sudo ./saveimg.sh kubesphere-dev"
#	ssh -i private_key -o StrictHostKeyChecking=no root@192.168.99.10 "cd /vagrant/image/; sudo ./saveimg.sh kubesphere-node1"
fi

echo "kubesphere的访问门户
Console: http://192.168.99.10:30880
Account: admin
Password: P@88w0rd"
